package com.mall.huohuo.process.engine;

import com.mall.huohuo.process.NodeContext;
import com.mall.huohuo.process.Pipeline;
import com.mall.huohuo.process.container.NodeContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author zycoding
 * @version ProcessEngine.java, v 0.1 2020-08-17 21:21 zycoding
 */
@Component
public class ProcessEngine {

    @Autowired
    NodeContainer nodeContainer;

    public void process(NodeContext nodeContext) {
        Assert.notNull(nodeContext, "请求不能为空。");
        final String processName = nodeContext.getProcessName();
        Assert.notNull(processName, "流程名称不能为空。");
        Pipeline pipeline = getPipLine(processName);
        Assert.notNull(pipeline, String.format("通过流程名字未能找到对应的流程信息。name=%s", processName));
        nodeContext.setPipeline(pipeline).getPipeline().invoke(nodeContext);
    }

    private Pipeline getPipLine(String entryName) {
        return nodeContainer.getPipline(entryName);
    }

}
